package com.foosun.core.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.foosun.core.base.BaseAction;
import com.foosun.core.base.PageData;
import com.foosun.core.entity.SysLogs;
import com.foosun.core.entity.validator.SysLogsValidator;
import com.foosun.core.service.SysLogsService;

/**
 * Created by Nelin on 14-5-30.
 */
@Controller
@RequestMapping("/core/sysLogs")
public class SysLogsAction implements BaseAction<SysLogs> {

    @Autowired
    private SysLogsService sysLogsService;


    @RequestMapping("/list")
    public String list(Model model, PageData<SysLogs> pageData, HttpServletRequest request) throws Exception {
        pageData.setWebMap(request.getParameterMap());
        pageData = sysLogsService.findList(pageData);
        model.addAttribute("pageData", pageData);
        return "/oa/sysLogs/list";
    }

    @RequestMapping("/new")
    public String form(Model model) throws Exception {
    	SysLogs sysLogs = new SysLogs();
    	model.addAttribute(sysLogs);
        return "/oa/sysLogs/input";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") Integer id) throws Exception {
        SysLogs sysLogs = sysLogsService.findById(id);
        model.addAttribute(sysLogs);
        return "/oa/sysLogs/input";
    }


    @RequestMapping("/save")
    public String save(@ModelAttribute("sysLogs") SysLogs sysLogs, Errors errors, Model model) throws Exception {

    	String view = null;
		if (!errors.hasErrors()) {
			ValidationUtils.invokeValidator(new SysLogsValidator(), sysLogs,
					errors);
			if(errors.hasErrors()){
				model.addAllAttributes(errors.getAllErrors());
				model.addAttribute(sysLogs);
				view = "/oa/sysLogs/input";
			}else{
				view = "redirect:/core/sysLogs/list";
				sysLogsService.save(sysLogs);
			}
		}
		return view;
    }

    @RequestMapping("/delete/{id}")
    public String delete(Model model, @PathVariable("id") Integer id) throws Exception {
        sysLogsService.delete(id);
        return "redirect:/core/sysLogs/list";
    }

    @RequestMapping("/delete/all")
    public String deleteAll(Model model) throws Exception {
        sysLogsService.deleteAll();
        return "redirect:/core/sysLogs/list";
    }

	@Override
	public String save(Model model, SysLogs entity) throws Exception {
		return null;
	}
    
}
